/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Shop;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ShopService;
import com.rzico.core.entity.SysMch;
import com.rzico.core.service.SysMchService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.Chef;
import com.rzico.basics.service.ChefService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 名厨档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-12-15
 */
@Api(description = "名厨档案接口")
@RestController
@RequestMapping("/admin/chef")
public class ChefAdminController extends BaseController {

    @Autowired
    private ChefService chefService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private SysMchService sysMchService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询名厨档案
     *
     * @return
     */
    @ApiOperation("分页查询名厨档案")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "名字", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "meta", value = "菜系", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "beGoodAt", value = "擅长", dataType = "String", paramType = "query")
    })
    public CommResult<Chef> list(String name, String meta, String beGoodAt, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(name)) {
            params.put("name", name);
        }
        if (StringUtils.isNotEmpty(meta)) {
            params.put("meta", meta);
        }
        if (StringUtils.isNotEmpty(beGoodAt)) {
            params.put("beGoodAt", beGoodAt);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
//        SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
//        if (!sysMch.getMchType().equals(0))  {
            params.put("enterpriseId", enterprise.getId());
//        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Chef> list = chefService.selectList(params);
        PageResult<Chef> pageResult = new PageResult<Chef>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条名厨档案
     *
     * @return
    */
    @ApiOperation("查询单条名厨档案")
    @GetMapping("/find/{id}")
    public CommResult<Chef> find(@PathVariable String id) {
        Chef result = chefService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("chef",result);

        Enterprise enterprise = enterpriseService.selectByPrimaryKey(result.getEnterpriseId());
        Shop shop = shopService.findByPoint(enterprise,0d,0d);
        data.put("shopId",shop.getId());
        data.put("shopName",shop.getName());


        return CommResult.success(data);

    }

    /**
     * 保存名厨档案
     *
     * @param chef
     * @return
     */
    @ApiOperation("保存名厨档案")
    @PostMapping("/save")
    public CommResult<Chef> save(@RequestBody Chef chef) {

        Enterprise enterprise = enterpriseService.getCurrent();
        SysMch sysMch = sysMchService.selectByPrimaryKey(enterprise.getMchId());
//        if (!sysMch.getMchType().equals(0))  {
            chef.setEnterpriseId(enterprise.getId());
//        }

        int affectCount = chefService.insertUseGeneratedKeys(chef);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Chef result = chefService.findById(chef.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除名厨档案
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除名厨档案", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除名厨档案,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Chef> del(@PathVariable String ids) {

        chefService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新名厨档案
     *
     * @param chef
     * @return
     */
    @Log(desc = "更新名厨档案", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新名厨档案")
    @PostMapping("/update")
    public CommResult<Chef> update(@RequestBody Chef chef) {
        int affectCount = chefService.updateByPrimaryKeySelective(chef);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Chef result = chefService.findById(chef.getId());
        return CommResult.success(result);
    }

}
